// src/utils/errorTypes.ts

/**
 * 应用错误基类
 */
export class AppError extends Error {
  public readonly code: string;
  public readonly userMessage: string;
  public readonly severity: 'low' | 'medium' | 'high';
  public readonly recoverable: boolean;

  constructor(
    code: string,
    message: string,
    userMessage: string,
    severity: 'low' | 'medium' | 'high' = 'medium',
    recoverable: boolean = true
  ) {
    super(message);
    this.name = 'AppError';
    this.code = code;
    this.userMessage = userMessage;
    this.severity = severity;
    this.recoverable = recoverable;
  }
}

/**
 * 图片加载错误
 */
export class ImageLoadError extends AppError {
  constructor(message: string, userMessage?: string) {
    super(
      'IMAGE_LOAD_FAILED',
      message,
      userMessage || '图片加载失败，请检查文件格式或重新上传',
      'medium',
      true
    );
  }
}

/**
 * 图片处理错误
 */
export class ImageProcessError extends AppError {
  constructor(message: string, userMessage?: string) {
    super(
      'IMAGE_PROCESS_FAILED',
      message,
      userMessage || '图片处理失败，请尝试调整参数或使用其他图片',
      'medium',
      true
    );
  }
}

/**
 * 文件类型错误
 */
export class FileTypeError extends AppError {
  constructor(allowedTypes: string[]) {
    super(
      'INVALID_FILE_TYPE',
      `Unsupported file type. Allowed: ${allowedTypes.join(', ')}`,
      `不支持的文件格式，请上传 ${allowedTypes.join('、')} 格式的图片`,
      'low',
      true
    );
  }
}

/**
 * 文件大小错误
 */
export class FileSizeError extends AppError {
  constructor(maxSizeMB: number) {
    super(
      'FILE_TOO_LARGE',
      `File size exceeds ${maxSizeMB}MB limit`,
      `文件大小超过 ${maxSizeMB}MB 限制，请选择较小的文件`,
      'low',
      true
    );
  }
}

/**
 * 浏览器兼容性错误
 */
export class BrowserCompatibilityError extends AppError {
  constructor(feature: string) {
    super(
      'BROWSER_INCOMPATIBLE',
      `Browser doesn't support: ${feature}`,
      `您的浏览器不支持 ${feature} 功能，请使用现代浏览器`,
      'high',
      false
    );
  }
}

/**
 * 网络错误
 */
export class NetworkError extends AppError {
  constructor() {
    super(
      'NETWORK_ERROR',
      'Network request failed',
      '网络连接失败，请检查网络设置后重试',
      'medium',
      true
    );
  }
}
